'''
显示列表数据 QListview 控件

model -> viewer -> Controller

扩展控件 QlistWidget, QLableWidgit
'''
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys

class TableView(QWidget):
    def __init__(self):
        super(TableView, self).__init__()

        self.setWindowTitle("QTableView 表格视图控件演示")

        self.resize(400, 600)
        self.model = QStandardItemModel(4, 3)
        self.model.setHorizontalHeaderLabels(['id', '姓名', '年龄'])
        self.tableview =QTableView()

        item11 = QStandardItem('10')
        item12 = QStandardItem('20')
        item13 = QStandardItem('30')

        self.model.setItem(0, 0, item11)
        self.model.setItem(0, 1, item12)
        self.model.setItem(0, 2, item13)


        self.tableview.setModel(self.model)



        listview = QListView()
        listModel = QStringListModel()
        self.list = ["one", 'two', 'three']

        listModel.setStringList(self.list)
        listview.setModel(listModel)
        listview.clicked.connect(self.clicked)


        self.listWidget = QListWidget()
        self.listWidget.addItem("item1")
        self.listWidget.addItem("item2")


        self.tableWiget = QTableWidget()
        self.tableWiget.setRowCount(3)
        self.tableWiget.setColumnCount(3)
        self.tableWiget.setHorizontalHeaderLabels(["id", "years", "nations"])
        getItem0 = QTableWidgetItem("Yang lan")
        self.tableWiget.setItem(0, 0, getItem0)

        self.tableWiget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWiget.resizeRowsToContents()
        self.tableWiget.resizeColumnsToContents()


        layout = QVBoxLayout()
        layout.addWidget(self.tableview)
        layout.addWidget(listview)
        layout.addWidget(self.listWidget)
        layout.addWidget(self.tableWiget)
        self.setLayout(layout)

    def clicked(self, item):
        QMessageBox.information(self, "QlistView", "您选择了" + self.list[item.row()])

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = TableView()
    ex.show()
    sys.exit(app.exec_())